package com.liao.furns.web; /**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */

import com.google.gson.Gson;
import com.liao.furns.pojo.Admin;
import com.liao.furns.pojo.Cart;
import com.liao.furns.pojo.CartItem;
import com.liao.furns.pojo.Furn;
import com.liao.furns.service.FurnService;
import com.liao.furns.service.impl.FurnServiceImpl;
import com.liao.furns.utils.DataUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "CartServlet", value = "/CartServlet")
public class CartServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();

    public void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart =(Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            cart.clear();
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    public void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = DataUtils.parseInt(request.getParameter("id"),0);
        Furn furn = furnService.queryFurnById(id);
        String referer = request.getHeader("Referer");
        if(furn.getStock() == 0){
            response.sendRedirect(referer);
            return;
        }
        CartItem item = new CartItem(furn.getId(),furn.getName(),furn.getPrice(),1,furn.getPrice());
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        if(cart == null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(item);
//        System.out.println("cart = " + cart);
        response.sendRedirect(referer);
    }

    public void addItemByAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = DataUtils.parseInt(request.getParameter("id"),0);
        Map<String,Object> resultMap = new HashMap<>();
        Furn furn = furnService.queryFurnById(id);
        String referer = request.getHeader("Referer");
        if(furn.getStock() == 0){
            response.sendRedirect(referer);
            return;
        }
        CartItem item = new CartItem(furn.getId(),furn.getName(),furn.getPrice(),1,furn.getPrice());
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        if(cart == null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(item);
        resultMap.put("furnCount",cart.getTotalCount());
        String resultJson = new Gson().toJson(resultMap);
        response.getWriter().write(resultJson);
    }

    public void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = DataUtils.parseInt(request.getParameter("id"),0);
        int count = DataUtils.parseInt(request.getParameter("count"), 1);
        Cart cart =(Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            cart.updateCount(id,count);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    public void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = DataUtils.parseInt(request.getParameter("id"),0);
        System.out.println("jinlaile");
        Cart cart =(Cart) request.getSession().getAttribute("cart");
        System.out.println(id);
        if(cart != null){
            cart.deleteItem(id);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

}
